/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Courseteacher;
import me.zhengjie.modules.system.service.CourseteacherService;
import me.zhengjie.modules.system.domain.vo.CourseteacherQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2025-01-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程教师管理")
@RequestMapping("/api/courseteacher")
public class CourseteacherController {

    private final CourseteacherService courseteacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('courseteacher:list')")
    public void exportCourseteacher(HttpServletResponse response, CourseteacherQueryCriteria criteria) throws IOException {
        courseteacherService.download(courseteacherService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程教师")
    @ApiOperation("查询课程教师")
//    @PreAuthorize("@el.check('courseteacher:list')")
    public ResponseEntity<PageResult<Courseteacher>> queryCourseteacher(CourseteacherQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(courseteacherService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增课程教师")
    @ApiOperation("新增课程教师")
//    @PreAuthorize("@el.check('courseteacher:add')")
    public ResponseEntity<Object> createCourseteacher(@Validated @RequestBody Courseteacher resources){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("courseId",resources.getCourseId());
        queryWrapper.eq("teacherId",resources.getTeacherId());
        courseteacherService.remove(queryWrapper);
        for(Integer item:resources.getChapterIds()){
            Courseteacher courseteacher = new Courseteacher();
            courseteacher.setCourseChapterId(item);
            courseteacher.setCourseId(resources.getCourseId());
            courseteacher.setTeacherId(resources.getTeacherId());
            courseteacherService.create(courseteacher);
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改课程教师")
    @ApiOperation("修改课程教师")
//    @PreAuthorize("@el.check('courseteacher:edit')")
    public ResponseEntity<Object> updateCourseteacher(@Validated @RequestBody Courseteacher resources){
        courseteacherService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程教师")
    @ApiOperation("删除课程教师")
//    @PreAuthorize("@el.check('courseteacher:del')")
    public ResponseEntity<Object> deleteCourseteacher(@RequestBody List<Integer> ids) {
        courseteacherService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}